package com.blb.community.controller;


import com.blb.community.Annotation.Log;
import com.blb.community.entity.SysDept;
import com.blb.community.entity.SysDictData;
import com.blb.community.service.ISysDeptService;
import com.blb.community.utils.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 部门表 前端控制器
 * </p>
 *
 * @author 帅帅
 * @since 2022-07-02
 */
@Api(tags = "部门管理")
@RestController
@RequestMapping("/sys-dept")
public class SysDeptController {

    @Autowired
    private ISysDeptService sysDeptService;

    /**
     * 部门列表查询
     *
     * @return
     */
    @ApiOperation("部门列表")
    @PostMapping("/SysDepts")
    public ResponseResult<List<SysDept>> listSysDept(@RequestBody SysDept sysDept) {
        List<SysDept> SysDepts = sysDeptService.getDeptList(sysDept);
        return ResponseResult.ok(SysDepts);
    }
    @Log(module = "添加部门", operationType = 1, requestMethod = "POST")
    @ApiOperation("添加部门")
    @PostMapping("/SysDept")
    public ResponseResult<String> addSysDept(@RequestBody SysDept sysDept) {

        String s = sysDeptService.addSysDept(sysDept);
        if (s.equals("添加成功")){
            return ResponseResult.ok("添加成功");
        } else{
            return ResponseResult.ok("添加失败");
        }
    }
    @Log(module = "修改部门", operationType = 2, requestMethod = "PUT")
    @ApiOperation("修改部门")
    @PutMapping("/SysDept")
    public ResponseResult<String> updateSysDept(@RequestBody SysDept sysDept) {

        String s = sysDeptService.updateDept(sysDept);
        if (s == "部门有人"){
            return ResponseResult.ok("部门有人");
        }
        if (s == "有子部门"){
            return ResponseResult.ok("有子部门");
        }
        if (s == "部门名已存在，不可重复"){
            return ResponseResult.ok("部门名已存在，不可重复");
        }
        return ResponseResult.ok("修改成功");
    }
    @Log(module = "删除部门", operationType = 3, requestMethod = "DELETE")
    @ApiOperation("删除部门")
    @DeleteMapping("/SysDept/{deptId}")
    public ResponseResult<String> deleteSysDept(@PathVariable long deptId){
        String s = sysDeptService.deleteDept(deptId);
        if (s==null){
        return ResponseResult.ok("删除成功");
        }else if (s == "有子部门"){
            return ResponseResult.ok("有子部门");
        }else {
            return ResponseResult.ok("部门有人");
        }
    }

    @ApiOperation("获取使用状态")
    @GetMapping("getDeptStauts")
    public ResponseResult<Object[]> getDeptStauts() {
        List<SysDictData> dict = sysDeptService.getDict((long) 3);
        return ResponseResult.ok(dict.toArray());
    }
}
